package com.itboye.ihomebank.db;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.MyApplcation;

/**
 * 保存草稿
 * 
 * @author admin
 * 
 */
public class DBManager {

	DBHelper mDbHelper;
	SQLiteDatabase db;
	Context mContext;
	private RecordSQLiteOpenHelper helper = new RecordSQLiteOpenHelper(
			MyApplcation.ctx);;
	public static final String DB_NAME = "china_city_name.db";
	public static final String DB_NAME2 = "itboye.db";
	public static final String DB_PATH = "/data"
			+ Environment.getDataDirectory().getAbsolutePath() + "/"
			+ "com.itboye.ihomebank"; // 存放路径
	public static final String DB_PATH2 = "/data"
			+ Environment.getDataDirectory().getAbsolutePath() + "/"
			+ "com.itboye.ihomebank"; // 存放路径;
	private final int BUFFER_SIZE = 400000;

	public DBManager(Context mContext) {
		this.mContext = mContext;
		db = openDateBase();
		// mDbHelper = new DBHelper(mContext);
	}

	// /***
	// * 添加进草稿
	// * @param uid
	// * @param title
	// * @param content
	// * @param time
	// * @param re
	// * @param img
	// * @return
	// */
	// public long addNote(String uid,String title, String content,String img) {
	// db = mDbHelper.getWritableDatabase();
	// ContentValues cv = new ContentValues();
	// cv.put("uid", uid);
	// cv.put("title", title);
	// cv.put("content", content);
	// cv.put("img", img);
	// long num = db.insert("mynote", null, cv);
	// db.close();
	// return num;
	// }
	//

	// /**
	// *
	// *
	// * @return
	// */
	// public ArrayList<TieZiCaoGaoBean> queryNote(String uid) {
	// ArrayList<TieZiCaoGaoBean> list = new ArrayList<TieZiCaoGaoBean>();
	// db = mDbHelper.getReadableDatabase();
	//
	// Cursor mCursor = db.query("mynote", null, "uid=?", new String[]{uid},
	// null, null, null);
	// if (mCursor != null) {
	// while (mCursor.moveToNext()) {
	// TieZiCaoGaoBean note = new TieZiCaoGaoBean();
	// String title = mCursor.getString(mCursor
	// .getColumnIndex("title"));
	// note.setTitle(title);
	// String content = mCursor.getString(mCursor
	// .getColumnIndex("content"));
	// String img = mCursor.getString(mCursor
	// .getColumnIndex("img"));
	// note.setId(uid);
	// note.setTitle(title);
	// note.setImg(img);
	// note.setContent(content);
	// list.add(note);
	// }
	// mCursor.close();
	// }
	// db.close();
	// return list;
	// }

	// /**
	// * 删除缓存
	// * @param id
	// * @param title
	// * @param content
	// * @return
	// */
	// public long delete(String uid) {
	// db = mDbHelper.getWritableDatabase();
	// long result=db.delete("mynote", "uid=?", new String[]{uid});
	// return result;
	// }
	//
	/**
	 * 被调用方法
	 */
	public SQLiteDatabase openDateBase() {
		openDateBase(DB_PATH + "/" + DB_NAME);
		return this.db;
	}

	/**
	 * 被调用方法
	 */
	public void closeDateBase() {
		if (this.db != null)
			this.db.close();

	}

	/**
	 * 打开数据库
	 * 
	 * @param dbFile
	 * @return SQLiteDatabase
	 * @author sy
	 */
	private SQLiteDatabase openDateBase(String dbFile) {
		File file = new File(dbFile);
		if (!file.exists()) {
			// // 打开raw中得数据库文件，获得stream流
			InputStream stream = mContext.getResources().openRawResource(
					R.raw.china_city_name);
			try {

				// 将获取到的stream 流写入道data中
				FileOutputStream outputStream = new FileOutputStream(dbFile);
				byte[] buffer = new byte[BUFFER_SIZE];
				int count = 0;
				while ((count = stream.read(buffer)) > 0) {
					outputStream.write(buffer, 0, count);
				}
				outputStream.close();
				stream.close();
				SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile,
						null);
				return db;
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return db;
	}

	// /产品中西

	/**
	 * 清空数据
	 */
	public void deleteData() {
		db = helper.getWritableDatabase();
		db.execSQL("delete from records");
		db.close();
	}

	/***
	 * 根据城市查询城市编码
	 * 
	 * @param cityName
	 * @return
	 */
	public String queryCityNo(String cityName) {
		// db = mDbHelper.getReadableDatabase();
		Cursor cursor = null;
		if (this.db != null)
			cursor = db.query("hat_city", new String[] { "cityID" }, "city=?",
					new String[] { cityName }, null, null, null);
		String cityCode = null;
		if (cursor != null) {
			while (cursor.moveToNext()) {
				cityCode = cursor.getString(0);
			}
			cursor.close();
			db.close();
		}
		Log.v("cityId", cityCode + "---------------");
		return cityCode;
	}
}
